#include <iostream>
#include <pthread.h>
#include <unistd.h>
#include "RingQueue.hpp"
using namespace std;

struct threaddata
{
    RingQueue<int> *prq;
    string name;
};
void* consumer(void* args)
{
    threaddata *p=static_cast<threaddata*>(args);
    while(true)
    {
        sleep(3);
        int t=0;
        p->prq->Pop(&t);
        cout<<p->name<<"消费了一个产品"<<t<<endl;
    }
}
int data=1;

void* producer(void* args)
{
    threaddata *p=static_cast<threaddata*>(args);

    while(true)
    {
        sleep(1);
        cout<<p->name<<"生产了一个产品"<<endl;
        p->prq->Equeue(data++);
    }
}

int main()
{
     RingQueue<int> *rq = new RingQueue<int>();
    threaddata td1;
    td1.name="producer-1";
    td1.prq=rq;

    threaddata td2;
    td2.name="producer-2";
    td2.prq=rq;

    threaddata td3;
    td3.name="consumer-1";
    td3.prq=rq;

    threaddata td4;
    td4.name="consumer-2";
    td4.prq=rq;

    pthread_t p1,p2,c1,c2;
    pthread_create(&p1,nullptr,producer,(void*)&td1);
    pthread_create(&p2,nullptr,producer,(void*)&td2);
    pthread_create(&c1,nullptr,consumer,(void*)&td3);
    pthread_create(&c2,nullptr,consumer,(void*)&td4);

    pthread_join(p1,nullptr);
    pthread_join(p2,nullptr);
    pthread_join(c1,nullptr);
    pthread_join(c2,nullptr);

    return 0;
}